package com.liugh.util;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**PDF下载加水印功能 .
 * Created by Fatao on 2018/11/1 .
 */
public class PDFDownloadWorkUtil {

   private static final Log logger = LogFactory.get();
//    public  boolean waterMark(String inputFile, String outputFile, String waterMarkName) {
//        boolean isSuccess = false;
//        try {
//            PdfReader reader = new PdfReader(inputFile);
//            isSuccess = true;
//        } catch (IOException e1) {
//            e1.printStackTrace();
//            Logger.info("增加水印失败");
//        }
//        return isSuccess;
//    }

    /**
     * 通过读取本地文件加水印
     * @param pdfReader
     * @param outputFile
     * @param markName
     * @throws IOException
     * @throws DocumentException
     */
    public  void handleWaterMark(PdfReader pdfReader,String outputFile,String markName) throws IOException, DocumentException {
        PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream(outputFile));
        //这里的字体设置比较关键，这个设置是支持中文的写法
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
        int total = pdfReader.getNumberOfPages() + 1;

        PdfContentByte under;
        Rectangle pageRect = null;
        for (int i = 1; i < total; i++) {
            pageRect = stamper.getReader().getPageSizeWithRotation(i);

            // 获得PDF最顶层
            under = stamper.getOverContent(i);
            under.saveState();
            // set Transparency
            PdfGState gs = new PdfGState();
            // 设置透明度为0.2
            gs.setFillOpacity(0.5f);

            under.setGState(gs);
            under.restoreState();
            under.beginText();
            under.setFontAndSize(base, 36);
            under.setColorFill(BaseColor.LIGHT_GRAY);
            // 计算水印X,Y坐标
            float x = pageRect.getWidth() - 72;
            float y = pageRect.getHeight() - 72;
            // 水印文字成45度角倾斜
            under.showTextAligned(Element.ALIGN_CENTER, markName, x, y, 315);

            // 添加水印文字
            under.endText();
            under.setLineWidth(1f);
            under.stroke();
        }
        stamper.close();
    }

    /**
     * 通过URL读取文件增加水印
     * @param url
     * @param outputFile
     * @param waterMarkName
     * @return
     * @throws IOException
     */
    public  boolean waterMarkUrl(URL url,String outputFile, String waterMarkName) throws IOException {
        PdfReader pdfReader = new PdfReader(url);
        boolean isSuccess = false;
        try {
            handleWaterMark(pdfReader,outputFile,waterMarkName);
            isSuccess = true;
        } catch (DocumentException e) {
            e.printStackTrace();
            logger.info("增加水印失败");
        }
        pdfReader.close();
        return isSuccess;
    }

}
